//
//  LGMedicineBallPatientCell.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/8/21.
//  药球患者Cell

#import "LGMedicineBallPatientCell.h"
#import "LGScrollView.h"
@interface LGMedicineBallPatientCell()
/**   */
@property (nonatomic , strong)UIView *baseView;
/**  忽略 */
@property (nonatomic , strong)UIButton *ignoreBtn;
/**   */
@property (nonatomic , strong)LGScrollView *baseScroll;
/**   */
@property (nonatomic , strong)UIView *baseView2;
/**   */
@property (nonatomic , strong)UILabel *nameLabel;
/**   */
@property (nonatomic , strong)UIImageView *vipView;
/**   */
@property (nonatomic , strong)UIButton *doctorBtn;
/**   */
@property (nonatomic , strong)UIButton *focusBtn;
/**   */
@property (nonatomic , strong)UILabel *stausLabel;
/**   */
@property (nonatomic , strong)UILabel *ageLabel;
/**   */
@property (nonatomic , strong)UILabel *numberLabel;
/**   */
@property (nonatomic , strong)UILabel *contentLabel;
/**   */
@property (nonatomic , strong)UILabel *operationLabel;
/**   */
@property (nonatomic , strong)UIView *planView;
/**   */
@property (nonatomic , strong)NSMutableArray *planArray;
/**   */
@property (nonatomic , assign)CGFloat leftMargin;
@end

@implementation LGMedicineBallPatientCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubView];
    }
    return  self;
}

-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    self.nameLabel.text = LGNSString(dataDic[@"patient_name"]);
    NSString *vipStr = LGNSString(dataDic[@"vip_icon_url"]);
    NSString *doctorName = LGNSString(dataDic[@"doctor_name"]);
    [self.vipView sd_setImageWithURL:[NSURL URLWithString:vipStr]];
    CGFloat left = (vipStr.length > 0)?viewPix(24):viewPix(4);
    CGFloat width = [doctorName textWidthWithFont:LGFontWeight(12, UIFontWeightMedium) maxHeight:MAXFLOAT];
    width = (doctorName.length>0)?width+viewPix(35):0;
    [self.doctorBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLabel.mas_right).offset(left);
        make.width.equalTo(@(width));
    }];
    [self.doctorBtn setTitle:doctorName forState:UIControlStateNormal];
    self.doctorBtn.hidden = (doctorName.length>0)?NO:YES;
    self.stausLabel.text = LGNSString(dataDic[@"status_name"]);
    NSString *isexpire = LGNSString(dataDic[@"isexpire"]);
    //排队中-1 、未完成-3 、已完成-4 
    NSString *status = LGNSString(dataDic[@"status"]);
    if([status isEqualToString:@"1"]){
        //排队中、待安排
        self.stausLabel.textColor = [UIColor colorWithString:@"#3291FF"];
    }else if ([status isEqualToString:@"3"] && [isexpire isEqualToString:@"1"]){
        //未完成-已过期-红色
        self.stausLabel.textColor = [UIColor colorWithString:@"#DD3030"];
    }else if ([status isEqualToString:@"3"] && [isexpire isEqualToString:@"0"]){
        //未完成-未到时间-蓝色
        self.stausLabel.textColor = [UIColor colorWithString:@"#3291FF"];
    }else{
        //已完成、已忽略
        self.stausLabel.textColor = [UIColor colorWithString:@"#999999"];
    }
    
    //性别、年龄、病案号
    NSString *gender = LGNSString(dataDic[@"gender"]);
    NSString *age = LGNSString(dataDic[@"age"]);
    NSString *number = LGNSString(dataDic[@"hospital_bano"]);
    if(gender.length>0 && age.length >0){
        self.ageLabel.text = [NSString stringWithFormat:@"%@ | %@岁 %@",gender,age,number];
    }else if (gender.length > 0 && age.length == 0){
        self.ageLabel.text = [NSString stringWithFormat:@"%@ %@",gender,number];
    }else if (gender.length == 0 && age.length>0){
        self.ageLabel.text = [NSString stringWithFormat:@"%@岁 %@",age,number];
    }else{
        self.ageLabel.text = [NSString stringWithFormat:@"%@",number];
    }
    
    //备注
    NSString *content = LGNSString(dataDic[@"remarks"]);
    self.contentLabel.text = content;
    CGFloat contentTop = (gender.length == 0 && age.length == 0 && number.length == 0)?0:viewPix(10);
    [self.contentLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.ageLabel.mas_bottom).offset(contentTop);
    }];
    
    //入院计划
    NSString *typeName = LGNSString(dataDic[@"plan_type_name"]);
    self.operationLabel.text = typeName;
    CGFloat operationTop = (content.length == 0) ? 0 : viewPix(10);
    CGFloat typeWidth = [typeName textWidthWithFont:LGFontWeight(14, UIFontWeightMedium) maxHeight:MAXFLOAT];
    [self.operationLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentLabel.mas_bottom).offset(operationTop);
        make.width.equalTo(@(typeWidth+5));
    }];
    NSArray *planArray = dataDic[@"hospital_plan"];
    CGFloat planHeight = (planArray.count>0)?planArray.count*viewPix(21):viewPix(21);
    [self.planView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(planHeight));
    }];
    
    for (UILabel *label in self.planArray) {
        [label removeFromSuperview];
    }
    [self.planArray removeAllObjects];
    for (NSInteger i=0; i<planArray.count; i++) {
        NSDictionary *tempDic = planArray[i];
        UILabel *contentLabel = [UILabel lableWithText:[NSString stringWithFormat:@"%@ %@",tempDic[@"title"],tempDic[@"date"]] colorString:@"#333333" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:1];
        [self.planView addSubview:contentLabel];
        [self.planArray addObject:contentLabel];
        [contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.planView).offset(viewPix(21)*i);
            make.left.right.equalTo(self.planView);
            make.height.equalTo(@(viewPix(21)));
        }];
    }
    [self layoutIfNeeded];
}

-(void)setIndex:(NSInteger)index{
    _index = index;
}

-(void)setIsEditing:(BOOL)isEditing{
    _isEditing = isEditing;
    CGFloat leftMargin = (isEditing == YES)?viewPix(44):viewPix(12);
    [self.nameLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.baseView2).offset(leftMargin);
    }];
    self.selectBtn.hidden = !isEditing;
}

//忽略
-(void)ignoreBtnAction{
    __weak typeof(self) weakSelf = self;
    [LGAlertTool alertWithTitle:@"忽略" content:@"确定忽略这条记录吗？" sureAction:^{
        if(weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(ignoreDataAction:index:)]){
            [weakSelf.delegate ignoreDataAction:LGNSString(weakSelf.dataDic[@"plan_id"]) index:weakSelf.index];
        }
    }];
}

//选择
-(void)selectedBtnAction:(UIButton *)sender{
    if(self.delegate && [self.delegate respondsToSelector:@selector(selectPlan:index:)]){
        [self.delegate selectPlan:!sender.selected index:self.index];
    }
}

#pragma mark
#pragma mark ====> 创建控件
-(void)layoutSubviews{
    [super layoutSubviews];
    self.baseScroll.contentSize = CGSizeMake(Screen_W+viewPix(18), self.baseScroll.bounds.size.height);
}
-(void)createSubView{
    self.contentView.backgroundColor = [UIColor colorWithString:@"#F2F3F5"];
    [self.contentView addSubview:self.baseView];
    [self.baseView addSubview:self.baseScroll];
    [self.baseScroll addSubview:self.ignoreBtn];
    [self.baseScroll addSubview:self.baseView2];
    [self.baseView2 addSubview:self.nameLabel];
    [self.baseView2 addSubview:self.vipView];
    [self.baseView2 addSubview:self.doctorBtn];
    [self.baseView2 addSubview:self.focusBtn];
    [self.baseView2 addSubview:self.stausLabel];
    [self.baseView2 addSubview:self.ageLabel];
    [self.baseView2 addSubview:self.numberLabel];
    [self.baseView2 addSubview:self.contentLabel];
    [self.baseView2 addSubview:self.operationLabel];
    [self.baseView2 addSubview:self.planView];
    [self.baseView2 addSubview:self.selectBtn];
    [self.baseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(viewPix(8));
        make.left.equalTo(self.contentView).offset(viewPix(16));
        make.right.equalTo(self.contentView).offset(-viewPix(16));
        make.bottom.equalTo(self.contentView).offset(-viewPix(8));
    }];
    [self.baseScroll mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.right.equalTo(self.baseView);
    }];
    [self.ignoreBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.baseView);
        make.left.equalTo(self.baseView2.mas_right);
        make.width.equalTo(@(viewPix(50)));
    }];
    [self.baseView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.baseScroll);
        make.top.bottom.equalTo(self.baseView);
        make.width.equalTo(@(Screen_W-viewPix(32)));
    }];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.baseView2).offset(viewPix(12));
        make.left.equalTo(self.baseView2).offset(viewPix(12));
    }];
    [self.vipView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLabel.mas_right).offset(viewPix(4));
        make.centerY.equalTo(self.nameLabel);
        make.width.height.equalTo(@(viewPix(16)));
    }];
    [self.doctorBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLabel.mas_right).offset(viewPix(4));
        make.centerY.equalTo(self.nameLabel);
        make.width.equalTo(@(viewPix(70)));
        make.height.equalTo(@(viewPix(22)));
    }];
    [self.stausLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.baseView2).offset(-viewPix(12));
        make.centerY.equalTo(self.nameLabel);
    }];
    [self.ageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameLabel.mas_bottom).offset(viewPix(10));
        make.left.equalTo(self.nameLabel);
    }];
    [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.ageLabel.mas_right).offset(viewPix(2));
        make.centerY.equalTo(self.ageLabel);
    }];
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.ageLabel.mas_bottom).offset(viewPix(10));
        make.left.equalTo(self.nameLabel);
        make.right.equalTo(self.baseView2).offset(-viewPix(12));
    }];
    [self.operationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentLabel.mas_bottom).offset(viewPix(9));
        make.left.equalTo(self.nameLabel);
        make.width.equalTo(@(viewPix(56)));
        make.height.equalTo(@(viewPix(21)));
    }];
    [self.planView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.operationLabel.mas_right).offset(viewPix(12));
        make.right.equalTo(self.baseScroll).offset(-viewPix(12));
        make.top.equalTo(self.operationLabel);
        make.bottom.equalTo(self.baseView2).offset(-viewPix(12));
    }];
    [self.selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.right.equalTo(self.contentView);
    }];
}

-(NSMutableArray *)planArray{
    if(!_planArray){
        _planArray = [NSMutableArray array];
    }
    return _planArray;
}
-(UIView *)baseView{
    if(!_baseView){
        _baseView = [[UIView alloc] init];
        _baseView.backgroundColor = [UIColor whiteColor];
        _baseView.cornerRidus = 15;
    }
    return _baseView;
}

-(UIButton *)ignoreBtn{
    if(!_ignoreBtn){
        _ignoreBtn = [UIButton buttonWithTitle:@"忽略" titleFont:LGFontWeight(10, UIFontWeightMedium) textColor:@"#FFFFFF" imageName:@""];
        _ignoreBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        _ignoreBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, viewPix(15));
        _ignoreBtn.backgroundColor = [UIColor colorWithString:@"#FFC42E"];
        [_ignoreBtn addTarget:self action:@selector(ignoreBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _ignoreBtn;
}

-(LGScrollView *)baseScroll{
    if(!_baseScroll){
        _baseScroll = [[LGScrollView alloc]init];
        _baseScroll.backgroundColor = [UIColor colorWithString:@"#FFC42E"];
        _baseScroll.showsHorizontalScrollIndicator = NO;
    }
    return _baseScroll;
}
-(UIView *)baseView2{
    if(!_baseView2){
        _baseView2 = [[UIView alloc] init];
        _baseView2.backgroundColor = [UIColor whiteColor];
    }
    return _baseView2;
}
-(UILabel *)nameLabel{
    if(!_nameLabel){
        _nameLabel = [UILabel lableWithText:@"" colorString:@"#333333" textFont:LGFontWeight(16, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _nameLabel;
}

-(UIImageView *)vipView{
    if(!_vipView){
        _vipView = [[UIImageView alloc]init];
    }
    return _vipView;
}

-(UIButton *)doctorBtn{
    if(!_doctorBtn){
        _doctorBtn = [UIButton buttonWithTitle:@"" titleFont:LGFontWeight(12, UIFontWeightMedium) textColor:@"#56BEA0" imageName:@"药球患者-医生"];
        _doctorBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _doctorBtn.imageEdgeInsets = UIEdgeInsetsMake(0, viewPix(10), 0, 0);
        _doctorBtn.titleEdgeInsets = UIEdgeInsetsMake(0, viewPix(12), 0, 0);
        _doctorBtn.backgroundColor = [UIColor colorWithString:@"#EBF8F3"];
        _doctorBtn.userInteractionEnabled = NO;
        _doctorBtn.cornerRidus = viewPix(11);
    }
    return _doctorBtn;
}

-(UIButton *)focusBtn{
    if(!_focusBtn){
        _focusBtn = [UIButton buttonWithTitle:@"已关注" titleFont:LGFontWeight(12, UIFontWeightMedium) normalColor:@"#999999" selectColor:@"#999999"];
        _focusBtn.backgroundColor = [UIColor colorWithString:@"#EBF8F3"];
        _focusBtn.userInteractionEnabled = NO;
        _focusBtn.cornerRidus = viewPix(11);
    }
    return _focusBtn;
}

-(UILabel *)stausLabel{
    if(!_stausLabel){
        _stausLabel = [UILabel lableWithText:@"" colorString:@"#999999" textFont:LGFontWeight(14, UIFontWeightMedium) textAlignment:NSTextAlignmentRight lines:1];
    }
    return _stausLabel;
}

-(UILabel *)ageLabel{
    if(!_ageLabel){
        _ageLabel = [UILabel lableWithText:@"" colorString:@"#999999" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _ageLabel;
}

-(UILabel *)numberLabel{
    if(!_numberLabel){
        _numberLabel = [UILabel lableWithText:@"" colorString:@"#999999" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _numberLabel;
}

-(UILabel *)contentLabel{
    if(!_contentLabel){
        _contentLabel = [UILabel lableWithText:@"" colorString:@"#999999" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _contentLabel;
}

-(UILabel *)operationLabel{
    if(!_operationLabel){
        _operationLabel = [UILabel lableWithText:@"" colorString:@"#333333" textFont:LGFontWeight(14, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _operationLabel;
}

-(UIView *)planView{
    if(!_planView){
        _planView = [[UIView alloc] init];
    }
    return _planView;
}

-(UIButton *)selectBtn{
    if(!_selectBtn){
        _selectBtn = [UIButton buttonWithImage:@"unselect24" selectImage:@"selected24"];
        _selectBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _selectBtn.imageEdgeInsets = UIEdgeInsetsMake(0, viewPix(28), 0, 0);
        _selectBtn.hidden = YES;
        [_selectBtn addTarget:self action:@selector(selectedBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _selectBtn;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
